﻿using System;
using System.Windows.Forms;
using ComputerVision.Common;
using ComputerVision.Forms;
using ComputerVision.LoaiXeForm;
using ComputerVision.Role;
using ComputerVision.User;
using ComputerVision.VeXeForm;
using System.Web.Security;


namespace ComputerVision
{
    public partial class frmMain : Form
    {

        public frmMain()
        {
            InitializeComponent();
            ShowHideMenu();

            this.WindowState = FormWindowState.Maximized;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Bounds = Screen.PrimaryScreen.Bounds;
        }

        #region Closed

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            //foreach (Form openForm in Application.OpenForms)
            //{
            //    openForm.Close();
            //}
            Application.Exit();
        }

        #endregion

        private void mnuInfo_Click(object sender, EventArgs e)
        {
            var frm = new frmInformation();
            frm.ShowDialog();
        }

        private void CloseMainForm(object sender, FormClosedEventArgs e)
        {
            while (Application.OpenForms.Count > 0)
            {
                Application.OpenForms[0].Close();
            }
        }

        private void mnuItemLogin_Click(object sender, EventArgs e)
        {
            var frm = new frmLogin();
            frm.Show();
            Hide();
        }

        private void mnuItemChangePassword_Click(object sender, EventArgs e)
        {
            var frm = new frmChangePassword();
            frm.ShowDialog();
        }

        private void mnuItemRoles_Click(object sender, EventArgs e)
        {
            Utilities.CloseAllOldForm();
            var frm = new frmRoleList();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void mnuItemUserInRole_Click(object sender, EventArgs e)
        {
            Utilities.CloseAllOldForm();
            var frm = new frmUserInRole();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void mnuItemInitCamera_Click(object sender, EventArgs e)
        {
            Utilities.CloseAllOldForm();
            var frm = new frmConfigCamera();
            frm.ShowDialog();
        }

        private void mnuItemListUser_Click(object sender, EventArgs e)
        {
            Utilities.CloseAllOldForm();
            var frm = new frmUserList();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void mnuLoaiXe_Click(object sender, EventArgs e)
        {
            Utilities.CloseAllOldForm();
            var frm = new frmLoaiXeList();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void mnuItemVeXe_Click(object sender, EventArgs e)
        {
            Utilities.CloseAllOldForm();
            var frm = new frmVeXeList();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void trainningCharacterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //var frm = new frmTrainningCharacter();
            //frm.MdiParent = this;
            //frm.WindowState = FormWindowState.Maximized;
            //frm.Show();
        }

        private void menuXeRa_Click(object sender, EventArgs e)
        {
            //Utilities.CloseOldFormByName("frmXeVao");
            Utilities.CloseAllOldForm();

            var frm = new frmXeRaNew();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void menuXeVao_Click(object sender, EventArgs e)
        {
           // Utilities.CloseOldFormByName("frmXeRaNew");
            Utilities.CloseAllOldForm();
            //GC.Collect();

            var frm = new frmXeVao();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();

        }

        public void ShowHideMenu()
        {
            //bool user = Roles.IsUserInRole(@"Người dùng");
            //bool admin = Roles.IsUserInRole(@"Quản trị");
            string[] roles = Roles.GetRolesForUser(Constants.CurrentUser);
            LicenseUtil license = new LicenseUtil();

            //show hide by license
            if (!license.VerifyLicense())
            {
                menuXeRa.Visible = false;
                menuXeVao.Visible = false;
                menuXevaoXera.Visible = false;
                editMenu.Visible = false;

                //hide sub menu
                mnuItemRoles.Visible = false;
                mnuItemUserInRole.Visible = false;
                mnuItemListUser.Visible = false;
                menuSaoLuu.Visible = false;

                //hide line
                toolStripMenuItem2.Visible = false;
            }
            else
            {
                menuXeRa.Visible = true;
                menuXeVao.Visible = true;
                menuXevaoXera.Visible = true;
                editMenu.Visible = true;

                //hide sub menu
                mnuItemRoles.Visible = true;
                mnuItemUserInRole.Visible = true;
                mnuItemListUser.Visible = true;
                menuSaoLuu.Visible = true;

                //hide line
                toolStripMenuItem2.Visible = true;
            }

            //show hide by roles
            if (roles.Length >= 1)
            {
                bool isQuanTri = false;
                foreach (string item in roles)
                {
                    if (item.Trim().ToLower().Equals(Constants.Roles.QuanTri))
                    {
                        isQuanTri = true;
                    }
                }

                if (!isQuanTri)
                {
                    mnuItemRoles.Visible = false;
                    mnuItemUserInRole.Visible = false;
                    mnuItemListUser.Visible = false;
                    menuSaoLuu.Visible = false;

                    //hide sub menu
                    menuBaoCao.Visible = false;

                    //hide line
                    toolStripMenuItem2.Visible = false;
                }
            }
            else
            {
                menuXeRa.Visible = false;
                menuXeVao.Visible = false;
                menuXevaoXera.Visible = false;
                editMenu.Visible = false;

                //hide sub menu
                mnuItemRoles.Visible = false;
                mnuItemUserInRole.Visible = false;
                mnuItemListUser.Visible = false;
                menuSaoLuu.Visible = false;
                mnuItemInitCamera.Visible = false;

                //hide line
                toolStripMenuItem2.Visible = false;
                toolStripMenuItem1.Visible = false;

            }
        }

        private void mnuSuCo_Click(object sender, EventArgs e)
        {
            Utilities.CloseAllOldForm();

            var frm = new frmSuCoVeXe();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void mnuKichHoat_Click(object sender, EventArgs e)
        {
            Utilities.CloseAllOldForm();

            var frm = new frmKichHoat(this);
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.ShowDialog();
        }

        private void menuXevaoXera_Click(object sender, EventArgs e)
        {
            Utilities.CloseAllOldForm();

            var frm = new frmXeVaoXeRa();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lblStatus.Text = @"Giờ: " + DateTime.Now.ToString("h:mm:ss tt");
            lblNgay.Text = @"Ngày: " + DateTime.Now.ToString("d/M/yyyy");
        }

        private void menuSaoLuu_Click(object sender, EventArgs e)
        {
            Utilities.CloseAllOldForm();
            var frm = new frmSaoLuu();
            frm.ShowDialog();
        }

        private void menuBaoCao_Click(object sender, EventArgs e)
        {
            Utilities.CloseAllOldForm();

            var frm = new frmBaoCao();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }
    }
}